package com.aeye.mbr.upms.rpc.sqlprovide;

import com.aeye.mbr.model.constant.UpmsDatabaseConstants;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * Created by chengshengru on 17-9-5.
 */
public class UpmsApiSqlProvide {
    public String selectUpmsPermissionByUpmsUserId(Map<String,Object> params) {
        return new SQL() {
            {
                this.SELECT("permission_id, system_id, pid, name, type, permission_value, uri, icon, status, orders");
                this.FROM(UpmsDatabaseConstants.TABLE_UPMS_PERMISSION + " up");
                this.WHERE("up.`status` = 1");
                this.WHERE("up.permission_id IN (" +
                        new SQL() {
                            {
                                this.SELECT("permission_id");
                                this.FROM(UpmsDatabaseConstants.TABLE_UPMS_ROLE_PERMISSION + " urp");
                                this.WHERE("urp.role_id IN (" + new SQL() {
                                    {
                                        this.SELECT("uur.role_id role_id");
                                        this.FROM(UpmsDatabaseConstants.TABLE_UPMS_USER_ROLE + " uur ");
                                        this.WHERE("uur.user_id = #{upmsUserId}");

                                    }
                                } + ")");


                            }
                        }.toString().concat(" UNION " + new SQL() {
                            {
                                //select permission_id from upms_user_permission uup1 where uup1.user_id=#{upmsUserId,jdbcType=INTEGER} and uup1.type=1
                                this.SELECT("permission_id");
                                this.FROM(UpmsDatabaseConstants.TABLE_UPMS_USER_PERMISSION + " uup1");
                                this.WHERE("uup1.user_id=#{upmsUserId}");
                                this.WHERE("uup1.type=1");
                            }
                        })
                        + ")");

                this.WHERE("up.permission_id NOT IN (" + new SQL() {
                    {
                        //select permission_id from upms_user_permission uup2 where uup2.user_id=#{upmsUserId,jdbcType=INTEGER} and uup2.type=-1
                        this.SELECT("permission_id");
                        this.FROM(UpmsDatabaseConstants.TABLE_UPMS_USER_PERMISSION + " uup2");
                        this.WHERE("uup2.user_id=#{upmsUserId}");
                        this.WHERE("uup2.type=-1");
                    }
                } + ")");
                this.ORDER_BY("up.orders asc");
            }
        }.toString();
    }

    public String selectUpmsRoleByUpmsUserId(Map<String,Object> params) {
        return new SQL() {
            {
                this.SELECT("role_id, name, title, description, ctime");
                this.FROM(UpmsDatabaseConstants.TABLE_UPMS_ROLE + " ur");
                this.WHERE("ur.role_id IN (" + new SQL() {
                    {
                        //select uur.role_id from upms_user_role uur where uur.user_id=#{upmsUserId,jdbcType=INTEGER}
                        this.SELECT("uur.role_id");
                        this.FROM(UpmsDatabaseConstants.TABLE_UPMS_USER_ROLE + " uur ");
                        this.WHERE("uur.user_id = #{upmsUserId}");
                    }
                } + ")");
            }
        }.toString();
    }

}
